package com.migo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.migo.mapper.ItemMapper;
import com.migo.pojo.EasyUIDataGridResult;
import com.migo.pojo.Item;
import com.migo.pojo.ItemDesc;
import com.migo.pojo.ItemParamItem;
import com.migo.utils.IDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Administrator on 2017/6/10 0010.
 */
@Service
public class ItemService extends BaseService<Item> {
    @Autowired
    private ItemDescService itemDescService;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemParamItemService itemParamItemService;
    private Logger logger = LoggerFactory.getLogger(ItemService.class);
    public Boolean saveItem(Item item,String desc,String itemParams){
        long id = IDUtils.genItemId();
        item.setId(id);
        item.setStatus(1);
        Integer save = save(item);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemDesc(desc);
        itemDesc.setItemId(id);
        Integer save1 = itemDescService.save(itemDesc);

        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setItemId(id);
        itemParamItem.setParamData(itemParams);
        Integer save2 = itemParamItemService.save(itemParamItem);

        return save==1&&save1==1&&save2==1;
    }

    /**
     * 查询商品列表
     */
    public EasyUIDataGridResult getItemList(Integer page,Integer rows){
        Example example = new Example(Item.class);
        example.orderBy("updated").desc();
        PageHelper.startPage(page,rows);
        List<Item> itemList = itemMapper.selectByExample(example);
        PageInfo<Item> pageInfo = new PageInfo<>(itemList);
        EasyUIDataGridResult result = new EasyUIDataGridResult(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }
    /**
     * 修改商品
     */
    public Boolean updateItem(Item item,String desc,String itemParams){
        item.setStatus(null);
        item.setCreated(null);
        Integer change1 = updateSelective(item);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        Integer change2 = itemDescService.updateSelective(itemDesc);

        Integer change3 = itemParamItemService.updateItemParamItem(item.getId(),itemParams);

        return change1==1&&change2==1&&change3==1;
    }
}
